package com.whalecloud.chart.service;

import com.alibaba.fastjson.JSONArray;
import com.whalecloud.chart.domain.DrawChartRequestDTO;
import com.whalecloud.chart.service.draw.*;
import com.whalecloud.chart.util.ChartTypeEnum;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author MarkAsh
 * @Date 2025/2/22
 **/
@Slf4j
public class DrawChartFactory {

    private static final Map<String, ChartService> CHART_SERVICE_MAP = new HashMap<>();

    static {
        // 3D柱状图
        CHART_SERVICE_MAP.put(ChartTypeEnum.BAR3D_CHART.getCode(), new Bar3DChartService());
        // 柱状图
        CHART_SERVICE_MAP.put(ChartTypeEnum.BAR_CHART.getCode(), new BarChartService());
        // 气泡图
        CHART_SERVICE_MAP.put(ChartTypeEnum.BUBBLE_CHART.getCode(), new BubbleChartService());
        // 折线图
        CHART_SERVICE_MAP.put(ChartTypeEnum.LINE_CHART.getCode(), new LineChartService());
        // 饼状图
        CHART_SERVICE_MAP.put(ChartTypeEnum.PIE_CHART.getCode(), new PieChartService());
        // 散点图
        CHART_SERVICE_MAP.put(ChartTypeEnum.SCATTER_CHART.getCode(), new ScatterPlotChartService());
        // 时许
        CHART_SERVICE_MAP.put(ChartTypeEnum.TIMELINE_CHART.getCode(), new TimeSeriesChartService());
        // XY散点图
        CHART_SERVICE_MAP.put(ChartTypeEnum.XY_CHART.getCode(), new XyChartService());
    }

    public static ChartService getDrawChartService(String chartType) {
        return CHART_SERVICE_MAP.get(chartType);
    }

    /**
     * 绘制图表
     * @param requestDTO 请求参数
     * @return 图片文件
     * */
    public static File drawChart(DrawChartRequestDTO requestDTO){
        return getDrawChartService(requestDTO.getChartType()).drawChartFile(requestDTO);
    }

    /**
     * 获取示例数据
     * @param chartType 图表类型
     * @return 示例数据
     * */
    public static DrawChartRequestDTO getSampleData(String chartType){
        JSONArray sampleData = getDrawChartService(chartType).getSampleData();
        return DrawChartRequestDTO.builder().chartType(chartType).title("测试样例数据")
                .sourceData(sampleData).build();
    }
}
